package cn.stylefeng.guns.czlm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.config.properties.GunsProperties;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.czlm.entity.TbUser;
import cn.stylefeng.guns.czlm.service.TbUserService;
import cn.stylefeng.guns.czlm.warpper.TbUserWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/user")
public class TbUserController extends BaseController {

    private static String PREFIX = "/modular/system/vip/";

    @Autowired
    private GunsProperties gunsProperties;

    @Autowired
    private TbUserService tbUserService;

    /**
     * 跳转到查看提现列表的页面
     */
    @RequestMapping("/money")
    public String moneyIndex() {
        return "/modular/system/money/money_list.html";
    }
    /**
     * 跳转到查看会员列表的页面
     * @author 铭君
     * @Date 2019/4/28 14:08
     */
    @RequestMapping("/user")
    public String userIndex() {
        return PREFIX + "user.html";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object findStudent(TbUser tbUser){
        Page<Map<String, Object>> ulist = this.tbUserService.selectUsers(tbUser);
        //StudentWrapper数据传到页面上需要进行一次封装
        Page<Map<String, Object>> warpper = new TbUserWrapper(ulist).wrap();
        return LayuiPageFactory.createPageInfo(warpper);
    }

    //获取回显信息
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Object getUserInfo(@RequestParam Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new RequestEmptyException();
        }
        TbUser tbUser = this.tbUserService.selectById(id);
        Map<String, Object> map = BeanUtil.beanToMap(tbUser);
        return ResponseData.success(map);
    }

    /**
     * 冻结用户
     *
     * @author fengshuonan
     * @Date 2018/12/24 22:44
     */
    @RequestMapping("/freeze")
    @ResponseBody
    public ResponseData freeze(Integer sid,TbUser tbUser) {
        if (sid != null) {
            tbUser.setStatus(sid);
        } else {
            tbUser.setStatus(0);
        }
            tbUserService.updateUserStatus(tbUser);
            return SUCCESS_TIP;

    }

    /**
     * 修改用户页面跳转
     */
    @RequestMapping("/user_edit")
    public String edit(Long id, Model model){
        TbUser tbUser = tbUserService.selectById(id);
        model.addAttribute("tbUser",tbUser);
        return PREFIX+"user_edit.html";
    }

    /**
     * 添加用户页面跳转
     * @return
     */
    @RequestMapping("/user_add")
    public String add(){
        return PREFIX+"user_add.html";
    }

    /**
     * 修改或添加用户
     */
    @RequestMapping("/editOrAdd")
    @ResponseBody
    public ResponseData editOrAdd(TbUser tbUser,Integer usergrade){
        if (tbUser.getId()==null||tbUser.getId()==0){
            tbUser.setStatus(0);
            tbUserService.insertTbUser(tbUser);
            return SUCCESS_TIP;
        }
        //修改
        tbUserService.updateTbUser(tbUser);
        return SUCCESS_TIP;
    }
}
